using System.Linq;
using System.Xml.Linq;

namespace Code5Systems.FormMaster.Business
{

	public partial class FormController
	{
		
#region List Item Related Methods
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Adds a ListItem Element to the given Control
		/// </summary>
		/// <param name="defXmlDoc">The Current Working Form XmlDocument</param>
		/// <param name="objFornControlInfo">The FormControlInfo Object to add the ListAItem Element to</param>
		/// <param name="Text">The Text for the ListItem</param>
		/// <param name="Value">The Value for the ListItem</param>
		/// <remarks>
		/// </remarks>
		/// <history>
		/// 	[steve]	4/30/2005	Created
		/// </history>
		/// -----------------------------------------------------------------------------
		public FMListItem AddListItem(XDocument xDoc, FormControlInfo objFornControlInfo, string Category, string Text, string Value, bool selected)
		{
			FMListItem oListItem = null;
			XElement xControl = default(XElement);
			XElement xListItems = default(XElement);
			XElement xListItem = default(XElement);
			
			xControl = this.GetControlElement(xDoc, objFornControlInfo.TableRow, objFornControlInfo.TableCol);
			
			if (xControl != null)
			{
				xListItems = xControl.Elements("ListItems").FirstOrDefault();
				if (xListItems == null)
				{
					xListItems = new XElement("ListItems");
					xControl.Add(xListItems);
				}
				xListItem = new XElement("ListItem");
				xListItem.Add(new XAttribute("Text", Text));
				xListItem.Add(new XAttribute("Value", Value));
				xListItem.Add(new XAttribute("Selected", selected.ToString().ToLower()));
				xListItem.Add(new XAttribute("Category", Category));
				xListItems.Add(xListItem);
				
				oListItem = new FMListItem(Text, Value);
				oListItem.Category = Category;
				oListItem.Selected = selected;
				
				objFornControlInfo.ListItems.Add(oListItem);
				
			}
			
			return oListItem;
			
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Updates the Text and Value Attributes for a ListItem Element
		/// </summary>
		/// <param name="defXmlDoc">The Current Working Form XmlDocument</param>
		/// <param name="objFornControlInfo">The FormControlInfo Object to update the ListItem Element for</param>
		/// <param name="Text">The Text for the ListItem</param>
		/// <param name="Value">The Value for the ListItem</param>
		/// <param name="AtIndex">The index of the ListItem Element</param>
		/// <remarks>
		/// </remarks>
		/// <history>
		/// 	[steve]	4/30/2005	Created
		/// </history>
		/// -----------------------------------------------------------------------------
		public void UpdateListItem(XDocument xDoc, FormControlInfo objFornControlInfo, string Category, string Text, string Value, bool selected, int AtIndex)
		{
			XElement xControl = default(XElement);
			System.Collections.Generic.IEnumerable<XElement> xListItems = default(System.Collections.Generic.IEnumerable<XElement>);
			XElement xListItem = default(XElement);
			XAttribute xAttr = default(XAttribute);
			
			xControl = this.GetControlElement(xDoc, objFornControlInfo.TableRow, objFornControlInfo.TableCol);
			if (xControl != null)
			{
				xListItems = xControl.Elements("ListItems").Elements("ListItem");
				if (!(xListItems.ElementAt(AtIndex) == null))
				{
					xListItem = xListItems.ElementAt(AtIndex);
					
					xAttr = xListItem.Attribute("Text");
					if (xAttr != null)
					{
						xAttr.Value = Text;
					}
					
					xAttr = xListItem.Attribute("Value");
					if (xAttr != null)
					{
						xAttr.Value = Value;
					}
					
					xAttr = xListItem.Attribute("Selected");
					if (xAttr != null)
					{
						xAttr.Value = (string) (selected.ToString().ToLower());
					}
					else
					{
						xListItem.Add(new XAttribute("Selected", selected.ToString().ToLower()));
					}
					
					xAttr = xListItem.Attribute("Category");
					if (xAttr != null)
					{
						xAttr.Value = Category;
					}
					else
					{
						xListItem.Add(new XAttribute("Category", Category));
					}
					
				}
			}
			
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Removes the ListItem Element as the given Index for the given control
		/// </summary>
		/// <param name="defXmlDoc">The Current Working Form XmlDocument</param>
		/// <param name="objFornControlInfo">The FormControlInfo Object to delete the ListItem Element from</param>
		/// <param name="AtIndex">The index of the ListItem Element</param>
		/// <remarks>
		/// </remarks>
		/// <history>
		/// 	[steve]	4/30/2005	Created
		/// </history>
		/// -----------------------------------------------------------------------------
		public void DeleteListItem(XDocument xDoc, FormControlInfo objFornControlInfo, int AtIndex)
		{
			//Dim oControlNode As XmlNode
			XElement xControl = default(XElement);
			//Dim oListItemNodes As XmlNodeList
			System.Collections.Generic.IEnumerable<XElement> xListItems = default(System.Collections.Generic.IEnumerable<XElement>);
			
			//oControlNode = defXmlDoc.SelectSingleNode("//Controls/Control[@TableRow=" & objFornControlInfo.TableRow.ToString() & "][@TableCol=" & objFornControlInfo.TableCol.ToString() & "]")
			xControl = this.GetControlElement(xDoc, objFornControlInfo.TableRow, objFornControlInfo.TableCol);
			if (xControl != null)
			{
				//oListItemNodes = oControlNode.SelectNodes("ListItems/ListItem")
				xListItems = xControl.Elements("ListItems").Elements("ListItem");
				
				if (!(xListItems.ElementAt(AtIndex) == null))
				{
					xListItems.ElementAt(AtIndex).Remove();
				}
			}
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Removes all ListItem Elements for a given Control
		/// </summary>
		/// <param name="defXmlDoc">The Current Working Form XmlDocument</param>
		/// <param name="objFornControlInfo">The FormControlInfo Object to delete all the ListItem Elements from</param>
		/// <remarks>
		/// </remarks>
		/// <history>
		/// 	[steve]	4/30/2005	Created
		/// </history>
		/// -----------------------------------------------------------------------------
		public void DeleteAllListItems(XDocument xDoc, FormControlInfo objFornControlInfo)
		{
			XElement xControl = default(XElement);
			XElement xListemItems = default(XElement);
			
			xControl = this.GetControlElement(xDoc, objFornControlInfo.TableRow, objFornControlInfo.TableCol);
			if (xControl != null)
			{
				xListemItems = xControl.Element("ListItems");
				if (xListemItems != null)
				{
					xListemItems.RemoveAll();
				}
			}
		}
		
#endregion
		
	}
	
}
